﻿using AntdUI;
using DataTurnClient.Interface;
using DataTurnClient.Model;
using MySqlX.XDevAPI.Common;
using ReactiveUI;
using Splat;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Documents;

namespace DataTurnClient.ViewModel
{
    public class SetViewModel: ReactiveObject
    {
        public ViewModelActivator Activator { get; }

        public IScheduler MainScheduler;

        #region Command
        public ReactiveCommand<Unit, Result<List<Setting>>> SelectSetCommand { get; }

        public ReactiveCommand<Setting, Result<int>> ChangeSetCommand { get; }
        public ReactiveCommand<Unit, Result<List<int>>> SaveSetCommand { get; }

        #endregion

        #region 属性
        public List<Setting> SettingList
        {
            get => _SettingList;
            set => this.RaiseAndSetIfChanged(ref _SettingList, value);
        }
        private List<Setting> _SettingList = new List<Setting>();

        public int Timer
        {
            get => _Timer;
            set => this.RaiseAndSetIfChanged(ref _Timer, value);
        }
        private int _Timer;

        public int Log
        {
            get => _Log;
            set => this.RaiseAndSetIfChanged(ref _Log, value);
        }
        private int _Log;

        public bool On
        {
            get => _On;
            set => this.RaiseAndSetIfChanged(ref _On, value);
        }
        private bool _On;

        public bool Run
        {
            get => _Run;
            set => this.RaiseAndSetIfChanged(ref _Run, value);
        }
        private bool _Run;

        public string Url
        {
            get => _Url;
            set => this.RaiseAndSetIfChanged(ref _Url, value);
        }
        private string _Url;

        public string SafeCode
        {
            get => _SafeCode;
            set => this.RaiseAndSetIfChanged(ref _SafeCode, value);
        }
        private string _SafeCode;

        #endregion

        public SetViewModel()
        {
            Activator = new ViewModelActivator();
            SelectSetCommand = ReactiveCommand.Create(SelectSetCommandImpl);
            ChangeSetCommand = ReactiveCommand.Create<Setting, Result<int>>(ChangeSetCommandImpl);
            SaveSetCommand = ReactiveCommand.Create(SaveSetCommandImpl);

        }

        private Result<List<Setting>> SelectSetCommandImpl()
        {
            Result<List<Setting>> result = new Result<List<Setting>>()
            {
                Code = 200,
                Message = "请求成功",
                Success = true
            };
            try
            {
                var service = Locator.Current.GetService<IDBHelperService>();
                using (var db = service.SqlSugarClient(Globals.DbConnect, Globals.DbType))
                {
                    var Setting = db.Queryable<Setting>().ToList();
                    foreach (var item in Setting)
                    {
                        if (item.SetName == "Timer")
                        {
                            Timer = Convert.ToInt32(item.SetValue);
                        }
                        if (item.SetName == "Log")
                        {
                            Log = Convert.ToInt32(item.SetValue);
                        }
                        if (item.SetName == "Run")
                        {
                            Run = Convert.ToInt32(item.SetValue) > 0 ? true:false ;
                        }
                        if (item.SetName == "On")
                        {
                            On = Convert.ToInt32(item.SetValue) > 0 ? true : false ;
                        }
                        if (item.SetName == "Url")
                        {
                            Url = item.SetValue;
                        }
                        if (item.SetName == "SafeCode")
                        {
                            SafeCode = item.SetValue;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = $"SelectSetCommandImpl 发生异常：{ex.Message}";
                result.Success = false;
            }
            return result;
        }

        private Result<int> ChangeSetCommandImpl(Setting set)
        {
            Result<int> result = new Result<int>()
            {
                Code = 200,
                Message = "请求成功",
                Success = true
            };
            try
            {
                var service = Locator.Current.GetService<IDBHelperService>();
                using (var db = service.SqlSugarClient(Globals.DbConnect, Globals.DbType))
                {
                    int change = db.Updateable<Setting>()
                            .SetColumns(it => new Setting { SetValue = set.SetValue })
                            .Where(x => x.SetName == set.SetName && x.SetGroup == set.SetGroup).ExecuteCommand();
                    result.Data = change;
                }
            }
            catch(Exception ex)
            {
                result.Code = 500;
                result.Message = $"ChangeSetCommandImpl 发生异常：{ex.Message}";
                result.Success = false;
            }
            return result;
        }

        private Result<List<int>> SaveSetCommandImpl()
        {
            Result<List<int>> result = new Result<List<int>>()
            {
                Code = 200,
                Message = "请求成功",
                Success = true
            };
            try
            {
                var service = Locator.Current.GetService<IDBHelperService>();
                using (var db = service.SqlSugarClient(Globals.DbConnect, Globals.DbType))
                {
                    List<int> changes = new List<int>();
                    changes.Add(
                        db.Updateable<Setting>()
                            .SetColumns(it => new Setting { SetValue = BoolConvertString(On) })
                            .Where(it => it.SetName == "On" && it.SetGroup == "DingSet").ExecuteCommand()
                            );
                    changes.Add(
                        db.Updateable<Setting>()
                            .SetColumns(it => new Setting { SetValue = Url })
                            .Where(it => it.SetName == "Url" && it.SetGroup == "DingSet").ExecuteCommand()
                            );
                    changes.Add(
                        db.Updateable<Setting>()
                            .SetColumns(it => new Setting { SetValue = SafeCode })
                            .Where(it => it.SetName == "SafeCode" && it.SetGroup == "DingSet").ExecuteCommand()
                            );

                    changes.Add(
                        db.Updateable<Setting>()
                            .SetColumns(it => new Setting { SetValue = BoolConvertString(Run) })
                            .Where(it => it.SetName == "Run" && it.SetGroup == "Base").ExecuteCommand()
                            );
                    changes.Add(
                        db.Updateable<Setting>()
                            .SetColumns(it => new Setting { SetValue = Timer.ToString() })
                            .Where(it => it.SetName == "Timer" && it.SetGroup == "Base").ExecuteCommand()
                            );
                    changes.Add(
                        db.Updateable<Setting>()
                            .SetColumns(it => new Setting { SetValue = Log.ToString() })
                            .Where(it => it.SetName == "Log" && it.SetGroup == "Base").ExecuteCommand()
                            );
                    result.Data = changes;
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = $"SaveSetCommandImpl 发生异常：{ex.Message}";
                result.Success = false;
            }
            return result;
        }

        private string BoolConvertString(bool isCheck)
        {
            return (isCheck ? 1 : 0).ToString();
        }
    }
}
